package cn.gudian.cms.Message

class LinkGroupController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [linkGroupInstanceList: LinkGroup.list(params), linkGroupInstanceTotal: LinkGroup.count()]
    }

    def create = {
        def linkGroupInstance = new LinkGroup()
        linkGroupInstance.properties = params
        return [linkGroupInstance: linkGroupInstance]
    }

    def save = {
        def linkGroupInstance = new LinkGroup(params)
        if (!linkGroupInstance.hasErrors() && linkGroupInstance.save()) {
            flash.message = "linkGroup.created"
            flash.args = [linkGroupInstance.id]
            flash.defaultMessage = "LinkGroup ${linkGroupInstance.id} created"
            redirect(action: "show", id: linkGroupInstance.id)
        }
        else {
            render(view: "create", model: [linkGroupInstance: linkGroupInstance])
        }
    }

    def show = {
        def linkGroupInstance = LinkGroup.get(params.id)
        if (!linkGroupInstance) {
            flash.message = "linkGroup.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "LinkGroup not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [linkGroupInstance: linkGroupInstance]
        }
    }

    def edit = {
        def linkGroupInstance = LinkGroup.get(params.id)
        if (!linkGroupInstance) {
            flash.message = "linkGroup.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "LinkGroup not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [linkGroupInstance: linkGroupInstance]
        }
    }

    def update = {
        def linkGroupInstance = LinkGroup.get(params.id)
        if (linkGroupInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (linkGroupInstance.version > version) {
                    
                    linkGroupInstance.errors.rejectValue("version", "linkGroup.optimistic.locking.failure", "Another user has updated this LinkGroup while you were editing")
                    render(view: "edit", model: [linkGroupInstance: linkGroupInstance])
                    return
                }
            }
            linkGroupInstance.properties = params
            if (!linkGroupInstance.hasErrors() && linkGroupInstance.save()) {
                flash.message = "linkGroup.updated"
                flash.args = [params.id]
                flash.defaultMessage = "LinkGroup ${params.id} updated"
                redirect(action: "show", id: linkGroupInstance.id)
            }
            else {
                render(view: "edit", model: [linkGroupInstance: linkGroupInstance])
            }
        }
        else {
            flash.message = "linkGroup.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "LinkGroup not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def linkGroupInstance = LinkGroup.get(params.id)
        if (linkGroupInstance) {
            try {
                linkGroupInstance.delete()
                flash.message = "linkGroup.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "LinkGroup ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "linkGroup.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "LinkGroup ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "linkGroup.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "LinkGroup not found with id ${params.id}"
            redirect(action: "list")
        }
    }

    def deleteAll = {
     def qstr = params.qstr
     if (qstr) {
       try {
         LinkGroup.executeUpdate("delete LinkGroup where id in(" + qstr + ")")
         flash.message = "delete successfully:" + qstr
         redirect(action: "list")
       } catch (Exception e) {
         flash.message = "delete failed:" + qstr
         redirect(action: "list")
       }
     }
}
}
